package com.asiainfo.iam.controller;
import com.asiainfo.iam.config.Result;
import com.asiainfo.iam.entity.dto.FullSyncRequest;
import com.asiainfo.iam.entity.dto.IncrementalSyncRequest;
import com.asiainfo.iam.producer.FullSyncProducer;
import com.asiainfo.iam.producer.IncrementalSyncProducer;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@Tag(name = "IAM-MQ数据同步服务API")
@RestController
@RequestMapping("/api/iam/mqsync")
@Api(tags = "IAM-MQ数据同步服务API")
// TODO 2
@ConditionalOnProperty(name = "enable.syn.controller", havingValue = "true")
public class SyncController {
    private static final Logger log = LoggerFactory.getLogger(SyncController.class);


    @Autowired
    private FullSyncProducer fullSyncProducer;

    @Autowired
    private IncrementalSyncProducer incrementalSyncProducer;


    /**
     * 执行全量数据同步
     * @param fullRequest
     * @return
     */
    @Operation(summary = "全量数据同步")
    @PostMapping("/full")
    public Result syncFullData(@RequestBody FullSyncRequest fullRequest){
        try {
            fullSyncProducer.syncFullData(fullRequest);

            log.info("全量数据全部发送成功！");
            return Result.success();
        } catch (Exception e) {
            log.error("全量数据同步失败", e);
            throw new RuntimeException("全量数据同步失败",e);
        }

    }

    /**
     * 执行增量数据同步
     * @param IncrRequest
     * @return
     */
    @Operation(summary = "增量数据同步")
    @PostMapping("/incr")
    public Result synIncrementaData(@RequestBody IncrementalSyncRequest IncrRequest){
        try {
            incrementalSyncProducer.syncIncrementalData(IncrRequest);
            log.info("增量数据同步成功");
            return Result.success();
        } catch (Exception e) {
            log.error("增量数据同步失败", e);
            throw new RuntimeException("增量数据同步失败：" + e.getMessage(), e);
        }

    }

}
